﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Security.Cryptography;
using Adf.Core.Db;
using Test.Implement;
using Test.Interface;
namespace Test.Service
{
    public class TestService
    {

        /// <summary>
        /// 创建系统接口
        /// </summary>
        /// <returns></returns>
        public static IAppFactory Instance()
        {
            return new AppFactoryImpl();
        }




        /// <summary>
        /// 自定义流水号
        /// 
        /// 2020.5.9
        /// </summary>
        /// <param name="cate">配置表主键</param>
        /// <param name="prefix">前缀</param>
        /// <param name="datetype">时间格式</param>
        /// <param name="infix"></param>
        /// <param name="indexLength"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public static string GetCurrnetSn(string cate, string prefix = "", string datetype = "", string infix = "", int indexLength = 3, string suffix = "")
        {

            string str1 = "";
            Dictionary<string, object> dicInfo = new Dictionary<string, object>();
            string sWhere = " cate=" + DbService.SetQuotesValue(cate, true);
            if (!DbService.Exists("adf_sequencenumber", sWhere))
            {
                dicInfo.Add("cate", (object)cate);
                dicInfo.Add("prefix", (object)prefix);
                dicInfo.Add("datetype", (object)datetype);
                dicInfo.Add("infix", (object)infix);
                dicInfo.Add("indexLength", (object)indexLength);
                dicInfo.Add("suffix", (object)suffix);
                dicInfo.Add("curindex", (object)0);
                if (DbService.Insert("adf_sequencenumber", dicInfo).RetStatus != 100)
                    return "";
            }
            int num1 = DbService.Max("adf_sequencenumber", "curindex", sWhere) + 1;
            if (DbService.Update("adf_sequencenumber", sWhere, new Dictionary<string, object>() { { "curindex", (object)num1 } }).RetStatus == 101)
            {
                if (!string.IsNullOrEmpty(prefix))
                    str1 += prefix;
                if (!string.IsNullOrEmpty(datetype))
                {
                    string str2 = DateTime.Now.ToString(datetype);
                    str1 += str2;
                }
                if (!string.IsNullOrEmpty(infix))
                    str1 += infix;
                //补位
                str1 = str1 + num1.ToString().PadLeft(indexLength, '0');

                if (!string.IsNullOrEmpty(suffix))
                    str1 += suffix;
            }
            return str1;
        }



     



    }

}
